package com.community.controller.login;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.community.entity.Result;
import com.community.entity.User;
import com.community.mapper.login.LoginMapper;
import com.community.service.login.LoginService;
import com.community.utils.JWT;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Slf4j
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    private JWT jwt;
    /**
     * 登陆功能
     * @return
     */
    @ResponseBody
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String mobile = user.getMobile();
        log.info("请求的电话号码为：{}",mobile);
        Map<String, String> data = new HashMap<>();
        if(loginService.exist(mobile))
        {
            String uuid = loginService.getId(mobile);
            //获取jwt令牌
            String token = JWT.generateToken(mobile, uuid);
            String refreshToken = JWT.generateRefreshToken(mobile, uuid);
            log.info("token的值为：",token);
            data.put("token", token);
            data.put("refreshToken", refreshToken);
            return Result.success(data);
        }else{
            register(mobile);
            String id = loginService.getId(mobile);
            String token = JWT.generateToken(mobile, id);
            String refreshToken = JWT.generateRefreshToken(mobile, id);
            data.put("token", token);
            data.put("refreshToken", refreshToken);
        }
        return Result.success(data);
    }


    /**
     *
     * @param mobile  电话
     */
    public void register(String mobile){
        String uuid = UUID.randomUUID().toString();
        String token = JWT.generateToken(mobile, uuid);
        String refreshToken = JWT.generateRefreshToken(mobile, uuid);
        log.info("uuid:{}",uuid);
        log.info("token:{}", token);
        log.info("refreshToken", refreshToken);
        loginService.register(mobile, uuid, token, refreshToken);
    }

    @ResponseBody
    @PostMapping("/refreshToken")
    public Result refresh(ServletRequest req, ServletResponse res){

        HttpServletRequest httpServletRequest = (HttpServletRequest) req;
        String token = httpServletRequest.getHeader("Authorization");

        //待完善部分
        //如何refreshToken也过期.....
        if(token != null && !token.isEmpty())
        {
            DecodedJWT decodedJWT = JWT.verifyToken(token);
            String mobile = decodedJWT.getClaim("mobile").asString();
            String uuid = decodedJWT.getClaim("uuid").asString();

            // 生成新的访问令牌
            String newAccessToken = JWT.generateToken(mobile, uuid);
            String newRefreshToken = JWT.generateRefreshToken(mobile, uuid);

            loginService.refreshToken(uuid, newAccessToken, newRefreshToken);

            Map<String, String> data = new HashMap<>();
            data.put("token", newAccessToken);
            data.put("freshToken", newRefreshToken);

            return Result.success(data);
        }
        return Result.fail("");
    }
}
